package com.mai4j.maiadmin.controller.api.system;

import com.mai4j.maiadmin.misc.StructMapper;
import com.mai4j.maiadmin.model.RespCode;
import com.mai4j.maiadmin.model.dto.AdminMenuDTO;
import com.mai4j.maiadmin.model.dto.PageDTO;
import com.mai4j.maiadmin.model.http.AdminMenuGetReq;
import com.mai4j.maiadmin.model.http.AdminMenuItemReq;
import com.mai4j.maiadmin.model.http.AdminMenuSaveOrUpdateReq;
import com.mai4j.maiadmin.model.http.datatables.Datatable;
import com.mai4j.maiadmin.model.http.datatables.response.DatatableResp;
import com.mai4j.maiadmin.model.http.response.AdminMenuGetResp;
import com.mai4j.maiadmin.model.http.response.AdminMenuSaveOrUpdateResp;
import com.mai4j.maiadmin.model.http.response.SimpleResp;
import com.mai4j.maiadmin.service.AdminMenuService;
import com.mai4j.maiadmin.support.I18nProvider;
import com.mai4j.maiadmin.support.security.aop.annotation.RequiresAuthentication;
import com.mai4j.maiadmin.support.web.binding.ReqParam;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 微信公众号请关注: 开源螺丝钉
 * <br>
 * 码云请关注: <a href="https://gitee.com/xiyoufang">https://gitee.com/xiyoufang</a>
 * <br>
 * 哔哩哔哩请关注: <a href="https://space.bilibili.com/438927834">https://space.bilibili.com/438927834</a>
 *
 * @author xiyoufang
 */
@RequiresAuthentication
@RestController
@RequestMapping("/api/system/admin-menu")
public class ApiAdminMenuController {

    private final AdminMenuService adminMenuService;

    public ApiAdminMenuController(AdminMenuService adminMenuService) {
        this.adminMenuService = adminMenuService;
    }

    /**
     * 获取AdminMenuGetResp
     *
     * @param adminMenuGetReq adminMenuGetReq
     * @return AdminMenuGetResp
     */
    @PostMapping("/get")
    public ResponseEntity<AdminMenuGetResp> get(@RequestBody @Valid AdminMenuGetReq adminMenuGetReq) {
        AdminMenuDTO adminMenuDTO = adminMenuService.get(Long.parseLong(adminMenuGetReq.getAdminMenuId()));
        return ResponseEntity.ok(new AdminMenuGetResp()
                .setAdminMenu(StructMapper.INSTANCE.adminMenuDTOToAdminMenuVO(adminMenuDTO))
                .setCode(RespCode.OK.getCode())
                .setMessage(I18nProvider.get("text.successfully")));
    }

    @PostMapping("/datatable")
    public ResponseEntity<DatatableResp> datatable(@ReqParam Datatable datatable) {
        PageDTO<AdminMenuDTO> pageDTO = adminMenuService.paginate(datatable);
        return ResponseEntity.ok(new DatatableResp()
                .setDraw(datatable.getDraw())
                .setRecordsFiltered(pageDTO.getTotalRow())
                .setRecordsTotal(pageDTO.getTotalRow())
                .setData(pageDTO.getList()
                        .stream()
                        .map(StructMapper.INSTANCE::adminMenuDTOToAdminMenuVO)
                        .collect(Collectors.toList())));
    }

    /**
     * 规则验证
     *
     * @param adminMenuItemReq adminMenuItemReq
     * @return SimpleResp
     */
    @PostMapping("/validate")
    public ResponseEntity<SimpleResp> validate(@RequestBody @Valid AdminMenuItemReq adminMenuItemReq) {
        return ResponseEntity.ok(new SimpleResp()
                .setCode(RespCode.OK.getCode())
                .setMessage(I18nProvider.get("text.successfully")));
    }

    /**
     * @param adminMenuSaveOrUpdateReq adminMenuSaveOrUpdateReq
     * @return AdminMenuSaveResp
     */
    @PostMapping("/saveOrUpdate")
    public ResponseEntity<AdminMenuSaveOrUpdateResp> saveOrUpdate(@RequestBody @Valid AdminMenuSaveOrUpdateReq adminMenuSaveOrUpdateReq) {
        AdminMenuDTO adminMenuDTO = adminMenuService.saveOrUpdate(StructMapper.INSTANCE.adminMenuSaveOrUpdateReqToAdminMenuDTO(adminMenuSaveOrUpdateReq));
        return ResponseEntity.ok(new AdminMenuSaveOrUpdateResp()
                .setAdminMenuId(String.valueOf(adminMenuDTO.getAdminMenuId()))
                .setCode(RespCode.OK.getCode())
                .setMessage(I18nProvider.get(
                        Objects.equals(String.valueOf(adminMenuDTO.getAdminMenuId()), adminMenuSaveOrUpdateReq.getAdminMenuId())
                                ? "text.edit.successfully"
                                : "text.save.successfully")));
    }

}
